{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculation of temperatures in a pandapipes network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial describes how temperatures inside pipes can be calculated. In comparison to the hydraulic property calculation, additional parameters must be specified for the pipes: The heat transfer coefficient (`u_w_per_m2k`), which determines how well or poorly the pipe insulates. Likewise the ambient temperature of the pipe (`text_k`) can be changed, which is 293 K by default. To increase the spatial resolution of the pipe, more than one section can be defined using the parameter `sections`.\n",
    "\n",
    "Please note that only incompressible media should be used for the heating network calculation and the [`mode`](https://pandapipes.readthedocs.io/en/latest/pipeflow/calculation_modes.html#temperature-calculations-pipeflow-option-mode-all-or-mode-heat) in the `pipeflow` function has to be set to \"all\" or \"heat\". In case `mode` equals \"heat\", the user must manually specify a solution vector for the hydraulic calculations. It should also be noted that the temperature calculations are currently still sequential. This means that the calculated temperature values do not influence the fluid properties of the medium (density and viscosity). Therefore, the calculations are only valid if the properties are not very temperature-dependent or if there are minor changes in temperature.\n",
    "                                                                                                            \n",
    "In the following, a simple example for the creation and calculation of a network is presented. Here water is used as fluid and the mode \"all\" is selected."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"pics/heat_network_tutorial_example.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next commands import necessary modules. The component module is used to evaluate results of internal nodes at the end of the calculation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandapipes\n",
    "from pandapipes.component_models import Pipe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, a net container, the fluid and the junctions defined in the network are added."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create empty network\n",
    "net = pandapipes.create_empty_network(\"net\")\n",
    "\n",
    "# create fluid\n",
    "pandapipes.create_fluid_from_lib(net, \"water\", overwrite=True)\n",
    "\n",
    "# create junctions\n",
    "junction1 = pandapipes.create_junction(net, pn_bar=3, tfluid_k=290, name=\"Junction 1\")\n",
    "junction2 = pandapipes.create_junction(net, pn_bar=3, tfluid_k=290, name=\"Junction 2\")\n",
    "junction3 = pandapipes.create_junction(net, pn_bar=3, tfluid_k=290, name=\"Junction 3\")\n",
    "junction4 = pandapipes.create_junction(net, pn_bar=3, tfluid_k=290, name=\"Junction 4\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The external grid is specified with a `type` parameter \"pt\". This means that a temperature and a pressure are prescribed at the junction connected to the external grid. It would also be possible to set the type parameter to \"t\" or \"p\" only, having the effect that only the temperature or the pressure is fixed at the connected junction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create external grid\n",
    "pandapipes.create_ext_grid(net, junction=junction1, p_bar=6, t_k=363.15, name=\"External Grid\", type=\"pt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the next section, sinks and pipes are added. Note that several internal sections are used for the pipes and that a heat transfer coefficient is defined."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create sinks\n",
    "pandapipes.create_sink(net, junction=junction3, mdot_kg_per_s=1, name=\"Sink 1\")\n",
    "pandapipes.create_sink(net, junction=junction4, mdot_kg_per_s=2, name=\"Sink 2\")\n",
    "\n",
    "# create pipes\n",
    "pandapipes.create_pipe_from_parameters(net, from_junction=junction1, to_junction=junction2, length_km=0.1,\n",
    "                                       diameter_m=0.075, k_mm=0.025, sections=5, u_w_per_m2k=100,\n",
    "                                       text_k=298.15, name=\"Pipe 1\")\n",
    "\n",
    "pandapipes.create_pipe_from_parameters(net, from_junction=junction2, to_junction=junction3, length_km=2,\n",
    "                                       diameter_m=0.05, k_mm=0.025, sections=4, u_w_per_m2k=100,\n",
    "                                       text_k=298.15, name=\"Pipe 2\")\n",
    "\n",
    "pandapipes.create_pipe_from_parameters(net, from_junction=junction2, to_junction=junction4, length_km=1,\n",
    "                                       diameter_m=0.1, k_mm=0.025, sections=8, u_w_per_m2k=50,\n",
    "                                       text_k=298.15, name=\"Pipe 3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An additional note on the heat transfer coefficient: The unit of this coefficient is W/(m^2 $*$ K).\n",
    "Internally, this coefficient is multiplied with the pipes outer surface area ($\\pi * d * l$). Now\n",
    "the pipeflow can be run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run pipeflow\n",
    "pandapipes.pipeflow(net, stop_condition=\"tol\", iter=5, friction_model=\"colebrook\",\n",
    "                    mode=\"all\", transient=False, nonlinear_method=\"automatic\", tol_p=1e-4,\n",
    "                    tol_m=1e-4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The general results for the junctions and pipes can still be accessed as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_junction\n",
    "net.res_pipe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get the internal results regarding the division of the pipes into sections, use the following function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_1_results = Pipe.get_internal_results(net, [0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here the results of Pipe 1 (`[0]`) are accessed. In general these include three matrices with the values of `PINIT`, `VINIT` and `TINIT`. The internal results of the pipe can also be accessed separately, as shown here for Pipe 1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_1_results[\"VINIT_MEAN\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_1_results[\"PINIT\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_1_results[\"TINIT\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the pressures `PINIT` and the temperatures `TINIT` of the internal nodes of a pipe are `sections-1` values. For Pipe 1 they are 4, since `sections` is equal to 5 in this case. The internal flow velocities `VINIT` for each individual pipe section are therefore for `sections` pieces. The following figure illustrates this again, where the pipe is divided into `sections` equidistant segments and the orange lines represent the internal nodes:\n",
    "\n",
    "<img src=\"pics/heat_network_tutorial_pipe_sections.png\"> \n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
